import os
import sys
from datetime import datetime
import loguru

LOG_FILE_PATH = f"./agent_log/{datetime.now().strftime('%m%d-%H%M')}.log"


def is_sudo():
    uid = os.geteuid()
    return uid == 0


class MyLogger:
    def __init__(self, log_file_path=LOG_FILE_PATH):
        self.logger = loguru.logger
        self.logger.remove()
        self.logger.add(sys.stdout,
                        format="<green>{time:HH:mm:ss}</green> | "  # 颜色>时间
                               "{thread.name} | "  # 进程名
                               "<cyan>{module}</cyan>"  # 模块名
                               ":<cyan>{line}</cyan> | "  # 行号
                               "<level>{level}</level>: "  # 等级
                               "<level>{message}</level>",  # 日志内容
                        )
        # 输出到文件的格式,注释下面的add',则关闭日志写入
        self.logger.add(log_file_path, level='DEBUG',
                        format='{time:YYYYMMDD HH:mm:ss} - '  # 时间
                               "{process.name} | "  # 进程名
                               "{thread.name} | "  # 进程名
                               '{module}.{function}:{line} - {level} -{message}',  # 模块名.方法名:行号
                        rotation="10 MB")

    def get_logger(self):
        return self.logger


logger = MyLogger().get_logger()
